﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMMove.Models;
using VMMove.Business;
using System.ComponentModel;
using nVentive.Umbrella.Extensions;

namespace VMMove
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private VMListView _dataContext = new VMListView();

        public MainWindow()
        {
            InitializeComponent();

            _dataContext.NewMessage += AddMessage;

            DataContext = _dataContext;

            _dataContext.UpdateList();
        }

        private void unlinkButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _dataContext.UnlinkCurrent();
            }
            catch (Exception ex)
            {
                AddMessage(ex.ToString());
            }       
        }

        private void linkButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = new System.Windows.Forms.FolderBrowserDialog();

                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    _dataContext.LinkMachine(dialog.SelectedPath);
                }
            }
            catch (Exception ex)
            {
                AddMessage(ex.ToString());
            }
        }

        private void machinesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _dataContext.SelectedMachine = machinesListBox.SelectedItem as VirtualMachine;
        }

        private void AddMessage(string message)
        {
            messagesBox.AppendText(DateTime.Now + " : " + message + "\r\n");
        }

        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            _dataContext.UpdateList();
        }
    }
}
